package org.infodavid.common.impl.persistence;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.IBuilder;
import org.infodavid.common.dto.ITemplateVersionListItem;
import org.infodavid.common.model.ITemplateVersion;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.persistence.TemplateVersionDataService;

/**
 * The Class AbstractTemplateVersionDataService.
 */
public abstract class AbstractTemplateVersionDataService extends TemplateVersionDataService {

	/** The Constant log. */
	private static final Log LOGGER = LogFactory.getLog(AbstractTemplateVersionDataService.class);

	/** The builder. */
	private final IBuilder<ITemplateVersion> builder = ModelFactory.getInstance().getBuilder(
	    ITemplateVersion.class);

	/**
	 * The Constructor.
	 */
	protected AbstractTemplateVersionDataService() {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#create()
	 */
	public ITemplateVersion create() throws InstantiationException {
		return builder.create();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#copyTo(org.infodavid.common.model.IDataObject,
	 * boolean, boolean)
	 */
	public ITemplateVersion copyTo(final ITemplateVersion from, final boolean deep,
	    final boolean asNew) throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final ITemplateVersion to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setCategory(from.getCategory());
			to.setData(from.getData());
			to.setTemplate(from.getTemplate());
			to.setEncoding(from.getEncoding());
			to.setLanguage(from.getLanguage());
			to.setReference(from.getReference());
			to.setRevision(from.getRevision());

			if (from.getImages() == null) {}
			else {
				to.getImages().putAll(from.getImages());
			}
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.IDataService#copyListItemTo(org.infodavid.common.model.IDataObject
	 * , boolean, boolean)
	 */
	public ITemplateVersionListItem copyListItemTo(final ITemplateVersionListItem from,
	    final boolean deep, final boolean asNew) throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final ITemplateVersion to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setCategory(from.getCategory());
			to.setReference(from.getReference());
			to.setRevision(from.getRevision());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}
}
